Skip to main content

完整案例

完整的多平台发布

项目的构建流程是在项目git push --tags的时候,触发 workflow,通过Github Actions编译出来Windows、Linux、macOS三个操作系统对应的 64 位可执行文件,再根据tag nametag message来创建对应的Github Release,并将编译好的文件上传

name: .NET Core Release

on:
push:
# Sequence of patterns matched against refs/tags
tags:
- "v*" # Push events to matching v*, i.e. v1.0, v20.15.10

jobs:
build:

runs-on: ubuntu-latest

steps:
- name: Checkout source
uses: actions/checkout@v2

- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.101

- name: Install dependencies
run: dotnet restore

- name: Build
run: dotnet build --configuration Release --no-restore

- name: Zip the Build
run: zip -r ${{ secrets.ReleaseZipName }} ./AppName/bin/Release/netcoreapp3.1/

- name: Create Release and Upload Release Asset
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
tag_name: ${{ github.ref }}
name: Release ${{ github.ref }}
body: TODO New Release.
draft: false
prerelease: false
files: |
${{ secrets.ReleaseZipName }}.zip
LICENSE

Vue Press

on: 
push:
branches: [master] # 每当 push 到 master 分支时触发部署
workflow_dispatch: # 是否手动触发部署
jobs:
docs:
runs-on: ubuntu-latest # 指定运行所需要的虚拟机环境(必填)
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0 # “最近更新时间” 等 git 日志相关信息,需要拉取全部提交记录

- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: "14" # 选择要使用的 node 版本

# 缓存 node_modules
- name: Cache dependencies
uses: actions/cache@v2
id: yarn-cache
with:
path: |
**/node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-

# 如果缓存没有命中,安装依赖
- name: Install dependencies
if: steps.yarn-cache.outputs.cache-hit != 'true'
run: yarn --frozen-lockfile

# 运行构建脚本
- name: Build VuePress site
run: yarn docs:build

# 查看 workflow 的文档来获取更多信息
# @see https://github.com/crazy-max/ghaction-github-pages
- name: Deploy to GitHub Pages
uses: crazy-max/ghaction-github-pages@v2

# 环境变量
env:
GITHUB_TOKEN: ${{ secrets.ACTION_SECRET }}
with:
target_branch: gh-pages # 部署到 gh-pages 分支
build_dir: docs/.vuepress/dist # 部署目录为 VuePress 的默认输出目录

sbulime 插件

name: Release

env:
PACKAGE_NAME: "CpsRunCommands.sublime-package" # 注意文件名不能带空格

on:
push:
tags:
- 'v*'

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: msg
run: echo 'PACKAGE_NAME -> ${{env.PACKAGE_NAME}}'

- name: Checkout
uses: actions/checkout@v3

- name: Zip The File
uses: thedoctor0/zip-release@master
with:
type: 'zip'
filename: ${{env.PACKAGE_NAME}}
exclusions: '*.git* /*node_modules/* .editorconfig .github/ .gitignore/ .git/'

- name: ls
run: ls -al

- name: Upload the gihub Release
uses: softprops/action-gh-release@v1
with:
token: ${{ secrets.TOKEN }}
files: |
${{env.PACKAGE_NAME}}

release 发布文件

- name: Zip the Build
run: zip -r ${{ secrets.ReleaseZipName }} ./AppName/bin/Release/netcoreapp3.1/

- name: Create Release and Upload Release Asset
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
tag_name: ${{ github.ref }}
name: Release ${{ github.ref }}
body: TODO New Release.
draft: false
prerelease: false
files: |
${{ secrets.ReleaseZipName }}.zip
LICENSE

下载仓库文件

name: Download Repo

jobs:
download_repo:
name: start download repo files
runs-on: ubuntu-latest
steps:
- name: start ...
uses: actions/checkout@v2 # 这里也可以使用 v3版本 2022年截至

上传文件

steps:
- uses: actions/checkout@v2 # 下载仓库文件

- run: mkdir -p path/to/artifact # 创建一个空目录
- run: echo hello > path/to/artifact/world.txt # 创建一个新文件

- uses: actions/upload-artifact@v3 # 上传新的文件到仓库
with:
name: my-artifact
path: path/to/artifact/world.txt

配置node环境(setup-node@v3)

steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '14'
- run: npm install
- run: npm test

配置node环境,指定版本

name: Install Nodejs
env:
NODE_VERSION: "14"
jobs:
job-name:
runs-on: ubuntu-latest
steps:
- name: Setup Node.js (安装node环境)
uses: actions/setup-node@v1 # 第三方actions
with:
node-version: ${{env.NODE_VERSION}} # 选择要使用的 node 版本

- name: Cache dependencies # 缓存 node_modules
uses: actions/cache@v2
id: yarn-cache
with:
path: |
**/node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-

# 如果缓存没有命中,安装依赖
- name: Install dependencies
if: steps.yarn-cache.outputs.cache-hit != 'true'
run: yarn --frozen-lockfile

# 运行构建脚本
- name: Build VuePress site
run: yarn docs:build # 执行构建命令

配置 python 环境

jobs:
job-name:
steps:
- name: install Python
uses: actions/setup-python@v3

发布到 Git Pages


发布npm包

name: Publish Package to npmjs
on:
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Publish Package to npmjs
- uses: actions/checkout@v3
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v3
with:
node-version: '16.x' # 指定版本
registry-url: 'https://registry.npmjs.org' # 指定npm源
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

发布release

release:
name: Release Repo
runs-on: ubuntu-latest
steps:
- name: start
uses: actions/download-artifact@v2 # 下载指定的仓库文件

build node 项目

name: Release
on:
push:
tags:
- 'v*.*.*' # 所有带tag的推送时

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: install
run: |
npm i
npm run build

- name: Release
uses: softprops/action-gh-release@v1
with:
files: ./dist/*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

发布Github Pages

name: CI

on: [push]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run build and test
run: echo build and test
- name: Run a multi-line script
run: |
npm i
npm run build
npm run test


pages:
runs-on: ubuntu-latest
needs: [build]
steps:
- uses: actions/checkout@v2
- name: post pages
run: echo post pages
- name: Run a multi-line script
run: |
npm i
npm run build
npm run pages
- name: GitHub Pages
uses: crazy-max/ghaction-github-pages@v1.3.0
with:
build_dir: pages
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Matrix 矩阵 基础模板

jobs:
build:
strategy:
matrix:
# 指定node版本 矩阵指定多版本
node-version: [12.x, 14.x, 15.x]
# 指定os
os: ['ubuntu-latest', 'windows-latest', 'macos-latest']
# 用变量的形式,自动分配
runs-on: ${{ matrix.os }}

# 用变量的形式,自动分配
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

#有些任务是不能在其他环境运行的
# 比如GabrielBB/xvfb-action@v1 只能在linux 下使用
# 用if条件判断一下就好
- name: Run headless unit test
if: matrix.os == 'ubuntu-latest'
uses: GabrielBB/xvfb-action@v1
with:
run: |
yarn test
yarn e2e

# node 后续任务